// pages/minemodule/personalData/personalData.ts
const app = getApp()  // 获取App实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isqiye: true,
    zztempFilePaths: null,
    headtempFilePaths: null,
    zzstatus: "",
    userdata: {
      id: "",
      "name": "",
      "password": "",
      "phone": "",
      "email": "",
      "nikename": "",
      "homeaddr": "",
      "others": "",
      "zzimgs": "",//上传资质 多个key 用逗号分开
      "inctime": "",
    },
    nohead: app.getImgUrl('nohead.png')  // 调用App实例上的方法
  },
  on() {

  },
  giveup(e) {
    let name = "zzimgs"
    console.log(e.detail.returnval)
    console.log(" e.detail.returnval")
    this.setData({
      [`userdata.${name}`]: e.detail.returnval,
    });
  },
  onLoad() {
    this.getperson()
  },
  async uploadImage(type) {
    let img = null
    if (type == 1) {
      //头像
      img = this.data.headtempFilePaths
    } else {
      //资质证照
      img = this.data.zztempFilePaths
    }
    console.log(img)
    console.log(1243254)
    if (!img) {
      wx.showToast({
        title: '请先选择图片',
        icon: 'none'
      });
      return;
    }
    let res = await app.axios(
      'qiniu',
      {
        op: "qiniu_loadtuken",
      },
      false
    )
    const token = res.data;
    // 开始上传
    wx.uploadFile({
      url: 'https://up-z2.qbox.me', // 根据你的存储区域选择
      filePath: img,
      name: 'file',
      formData: {
        token: token,
      },
      success: (res) => {
        if (type == 1) {
          let datas = this.data.userdata
          datas.avatar = JSON.parse(res.data).key
          this.setData({
            userdata: datas
          })
        }
        else {
          let datas = this.data.userdata
          datas.zzimgs = JSON.parse(res.data).key
          this.setData({
            userdata: datas
          })
          // 这里可以获取到七牛云返回的文件地址
          const fileUrl = '你的域名/' + data.key;
        }


        wx.showToast({
          title: '上传成功',
          icon: 'success'
        });
      },
      fail: (err) => {
        console.error('上传失败', err);
        wx.showToast({
          title: '上传失败',
          icon: 'none'
        });
      }
    });
  },
  async save() {
    let datas = JSON.parse(JSON.stringify(this.data.userdata))
    datas.avatar = this.data.userdata.avatar.join(",")
    datas.zzimgs = this.data.userdata.zzimgs.join(",");
    let res = await app.axios(
      'user',
      {
        op: "ajaxUserDlUp",
        ...datas
      },
      false
    )
    if (res.code) {
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1500
      });
      wx.navigateBack();
    } else {
      wx.showToast({
        title: '修改失败',
        duration: 1500
      });
    }
  },
  async chackheadimg() {
    let returnval = await app.globalData.upimgJs.chackimg(1)
    let datas = this.data.userdata;
    datas.avatar = returnval.imgkeylist;
    this.setData({
      userdata: datas,
      headtempFilePaths: returnval.tempFilePaths[0]
    });
  },
  async chackimg() {
    let returnval = await app.globalData.upimgJs.chackimg(9)
    let datas = this.data.userdata
    datas.zzimgs = returnval.imgkeylist,
      this.setData({
        userdata: datas,
        zztempFilePaths: returnval.tempFilePaths
      });
  },
  handleInputChange: function (e) {
    const name = e.currentTarget.dataset.name;
    console.log(e.currentTarget)
    console.log(123324)
    // const { name } = e.currentTarget;
    const { value } = e.detail;

    console.log(`更新 ${name}:`, value);

    this.setData({
      [`userdata.${name}`]: value
    }, () => {
      console.log("当前 userdata 数据:", this.data.userdata);
    });

    // const { name } = e.currentTarget;
    // const { value } = e.detail;
    // console.log([`userdata.${name}`])
    // this.setData({
    //   [`userdata.${name}`]: value
    // });
  },
  async getperson() {
    //zzstatus  1企业用户，2客户
    if (app.globalData.session.zzstatus == 2) {
      this.setData({
        isqiye: false
      })
    } else {
      this.setData({
        isqiye: true
      })
    }
    console.log(app.globalData.session)
    console.log(6666666666)
    let res = await app.axios(
      'user',
      {
        op: "ajaxUserSets",
        zqstatus: app.globalData.session.zzstatus
      },
      false
    )

    let item = {
      avatar: res.item.avatar ? res.item.avatar.split(",") : '',
      id: res.item.infoid ? res.item.infoid : '',
      "name": res.item.name ? res.item.name : '',
      "password": res.item.password ? res.item.password : '',
      "email": res.item.email ? res.item.email : '',
      "nikename": res.item.nikename ? res.item.nikename : '',
      "homeaddr": res.item.homeaddr ? res.item.homeaddr : '',
      "others": res.item.others ? res.item.others : '',
      "zzimgs": res.item.zzimgs ? res.item.zzimgs.split(",") : '',//上传资质 多个key 用逗号分开
      "inctime": res.item.inctime ? res.item.inctime : '',//入职时间
    }
    console.log(item)
    this.setData({
      zzstatus: app.globalData.session.zzstatus,
      phone: res.item.phone ? res.item.phone : '',
      userdata: item
    })
  },
})